package main

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/namsral/flag"
	"go-demo/proto"
	"google.golang.org/grpc"
	"os"
)

var addr = flag.String("addr", ":8080", "address to listen")
var rpcAddr = flag.String("RPC", ":8088", "address to listen")

func main() {
	r := gin.Default()
	if os.Getenv("RPC") != "" {
		*rpcAddr = os.Getenv("RPC")
	}
	fmt.Println(*rpcAddr)
	r.GET("/hello", func(c *gin.Context) {
		name := c.DefaultQuery("name", "bobby")
		conn, err := grpc.Dial(*rpcAddr, grpc.WithInsecure())
		if err != nil {
			panic(err)
		}
		defer conn.Close()

		cs := proto.NewGreeterClient(conn)
		r, err := cs.SayHello(context.Background(), &proto.HelloRequest{Name: name})
		if err != nil {
			panic(err)
		}
		c.JSON(200, gin.H{
			"message": r.Message,
		})
	})
	r.Run(*addr)

}
